<?php
namespace app\web\controller\admin;

use app\admin\library\AdminController;
use cubox\Data;

class Setting extends AdminController{

    private $configPath = ROOT.DS.CONFIG.DS.'web';
    

    public function onIndex(){
        $this->power('web.setting');
        $this->pageTitle = '网站设置';
        $this->pageIcon = 'fa-cog';

        $template = new Data($this->configPath.DS.'template.php');
        $template->read();
        $this->set('template',$template->toArray());
        
        $base = new Data($this->configPath.DS.'base.php');
        $base->read();
        $this->set('base',$base->toArray());
        return $this->display();
    }

    public function onSaveTemplate(){
        $this->power('web.setting');
        //$data['asset'] =  $this->post('asset',$this->check()->type('string')->required());
        $data['index_template'] =  $this->post('index_template',$this->check()->type('string')->required());
        $data['part_template'] =  $this->post('part_template',$this->check()->type('string'));
        $data['article_template'] =  $this->post('article_template',$this->check()->type('string'));
        $data['error_template'] =  $this->post('error_template',$this->check()->type('string'));
        $data['page404_template']=$this->post('page404_template',$this->check()->type('string'));
        
        if (!file_exists($this->configPath)) mkdir($this->configPath,0777,true);
        
        $config = new Data($this->configPath.DS.'template.php');
       
        $config->read();

        foreach($data as $key => $value){
            $config->$key = $value;
        }
        
        $config->save();
   
        return true;
    }
    
    public function onSaveThemePath(){
        if (!file_exists($this->configPath)) mkdir($this->configPath,0777,true);
        $path =  $this->post('path',$this->check()->type('string')->required());

        $config = new Data($this->configPath.DS.'template.php');
        $config->read();
        $config->theme = $path;
        $config->save();
        
        return true;
    }


    public function onSaveBase(){
        $this->power('web.setting');

        $data['domain'] =  $this->post('domain',$this->check()->type('string')->required());
        $data['uploadFileUrl'] = $this->post('uploadFileUrl',$this->check('上传文件地址')->type('string'));
        $data['cache'] = $this->post('cache',$this->check()->type('bool'));
        $data['cacheTime'] = $this->post('cacheTime',$this->check()->type('int'));
        $data['webpackUrl'] = $this->post('webpackUrl',$this->check()->type('string'));
        
        if (!file_exists($this->configPath)) mkdir($this->configPath,0777,true);
        $config = new Data($this->configPath.DS.'base.php');
        $config->read();

        foreach($data as $key => $value){
            $config->$key = $value;
        }
        
        $config->save();
        
        return true;
    }
}